<template>
  <div class="app-container">
    <el-button type="primary" @click="addNav">添加</el-button>
    <el-table :data="dataList" style="width: 100%;margin-top:30px;" v-loading.body="listLoading" border>
      <el-table-column align="center" label="序号" width="100">
        <template scope="scope">
          {{scope.row.index}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="品类名称" width="200">
        <template scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>

      <el-table-column align="header-center" label="品类排序" width="200">
        <template scope="scope">
          {{scope.row.orderNum}}
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="300">
        <template scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑导航':'添加导航'">
      <el-form autoComplete="on" :model="nav" label-width="80px" label-position="left" ref="shopNav" :rules="navRules">

        <el-form-item label="品类名称" prop="name">
          <el-input autoComplete="on" name="name" v-model="nav.name" placeholder="品类名称"></el-input>
        </el-form-item>

        <el-form-item label="品类排序" prop="orderNum">
          <el-input autoComplete="on" name="orderNum" v-model="nav.orderNum" placeholder="品类排序"></el-input>
        </el-form-item>

      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmNav">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import { page, save } from 'api/shopCagegory'
  const category = {
    id: '',
    name: '',
    status: 0,
    orderNum: 0
  }
  export default {
    data() {
      return {
        dataList: [],
        nav: Object.assign({}, category),
        dialogVisible: false,
        dialogType: 'new',
        listLoading: true,
        checkStrictly: false,
        total: 10,
        defaultProps: {
          children: 'children',
          label: 'title'
        },
        listQuery: {
          page: 1,
          limit: 10,
          name: undefined
        },
        navRules: {
          name: [{
            required: true,
            message: '请输入品类名称',
            trigger: 'blur'
          }],
          orderNum: [{
            required: true,
            message: '请输入品类排序',
            trigger: 'blur'
          }, { pattern: /^\d+$/, message: '请输入数字', trigger: 'blur' }]
        }
      }
    },
    created() {
      this.getlist()
    },
    computed: {
    },
    methods: {
      getlist() {
        this.listLoading = true
        page(this.listQuery).then(response => {
          var data = response.data
          this.dataList = data.rows;
          this.total = data.total;
          this.listLoading = false;
          for (var ix in this.dataList) {
            this.dataList[ix].index = parseInt(ix, 10) + 1;
          }
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getlist()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getlist()
      },
      addNav() {
        this.dialogType = 'new'
        this.dialogVisible = true
        this.nav = Object.assign({}, category);
        this.nav.status = 1


      },
      handleEdit(scope) {
        this.dialogType = 'edit'
        this.dialogVisible = true
        this.nav.categoryId = scope.row.categoryId
        this.nav.categoryName = scope.row.categoryName
        this.nav.categoryHref = scope.row.categoryHref
        this.nav.categoryImage = scope.row.categoryImage
        this.nav.status = 1

      },
      confirmNav() {
        this.$refs.shopNav.validate(valid => {
          if (valid) {
            this.nav.status = 1
            save(this.nav).then(response => {
              if (response.status == 200) {
                this.$success('保存成功')
                this.dialogType = 'edit'
                this.dialogVisible = false
                this.getlist()
              } else {
                this.$error(response.message)
              }
            })
          }
        })
      }
    },

  }
</script>